/**
 * NOTE:
 * 前端与客户端通信
 * 基于webSocket封装，异步通信方法类
 */

class socket {
    // host：通信地址 callback：客户端->前端 通信信息传递回掉
    constructor( host, callback ) {
        // console.log( 'constructor', host, callback, this.webS );
        this.host = host||'127.0.0.1:28410';
        this.callback = callback||null;
        this.status = false; // 通信是否成功开启

        // 避免重复调用建议 WebSocket 通道
        if(!this.webS){
            this.webS = new WebSocket(`ws://${this.host}`);
        }
        
        this.onmessage = () => {
            return new Promise((resolve) => {
                this.webS.onmessage = (evt) => {
                    if(evt){
                        if(evt.data){
                            if(JSON.parse(evt.data)){
                                // console.log('onmessage', evt.data, this.callback, JSON.parse(evt.data).cmd, JSON.parse(evt.data).code );
                                if( JSON.parse(evt.data) )
                                // 触发快捷键截图
                                if(JSON.parse(evt.data).cmd === 'screenshot' && JSON.parse(evt.data).code.toString() === '1' && this.callback ){
                                    this.callback( 'setScreenKeyStatus', 1);
                                }
                                // esc 取消截图
                                else if(JSON.parse(evt.data).cmd == 'screenshot_over' && JSON.parse(evt.data).code.toString() === '2' && this.callback){
                                    this.callback( 'setScreenKeyStatus', 2);
                                }
                                // 导出的列表
                                else if(JSON.parse(evt.data).cmd == 'Download_result' && JSON.parse(evt.data).code.toString() === '8' && this.callback){
                                    this.callback( 'setFileDownloadList', JSON.parse(evt.data).param );
                                }
                                    
                                resolve (JSON.parse(evt.data));
                            }
                            else{
                                resolve (null);
                            }
                        }
                        else{
                            resolve (null);
                        }
                    }
                    else{
                        resolve (null);
                    }
                };
            });
        };

        this.error = () => {
            return new Promise((resolve) => {
                this.webS.error = () => {
                    this.status = false;
                    resolve ('webSocket class 通信连接失败！');
                    setTimeout(() => {
                        this.webSCOpen();
                    }, 500);
                };
            });
        };
    }

    webSCOpen = () => {
        if(this.webS.readyState !== 1)
        return new Promise((resolve, reject) => {
            console.log('准备通信！');
            this.webS.onopen = () => {
                this.status = true;
                resolve ('webSocket class 通信连接成功！');
            };
            this.error().then( evtData => {
                reject (evtData);
            });
        });
    };
    

    webSCAsync = (data="") => {
        return new Promise((resolve, reject) => {

            // console.log( 'webSCAsync', this.webS, data );
            this.webS.send(JSON.stringify(data));
    
            this.onmessage().then( evtData => {
                resolve ( evtData );
            });
    
            this.error().then( evtData => {
                reject (evtData);
            });
        });
    };

    webSCClose = () =>{
        return new Promise((resolve, reject) => {
            this.webS.close = () => {
                resolve ('webSocket class 通信关闭成功！');
            };

            this.error().then( evtData => {
                reject (evtData);
            });
        });
    };

}

export { socket }
